package com.cris.service;

import com.cris.helper.DatabaseHelper;
import com.cris.model.Product;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by owen on 2017/3/16.
 */
@Service
public class ProductService {

    /**
     * 获取产品列表
     */
    public List<Product> getProductList(){
        String sql = "SELECT * FROM product";
        return DatabaseHelper.queryEntityList(Product.class,sql);
    }

    /**
     * 模糊查询获取产品
     */
    public List<Product> getProductByFuzzySearch(String fuzzyName){
        String sql = "select * from product where name like '%"+fuzzyName+"%'";
        return DatabaseHelper.queryEntityList(Product.class,sql);
    }
    /**
     * 按照产品类型获得产品
     */
    public List<Product> getProductByTypeId(long typeId){
        //多表联合查询 根据parentid查到product
        String sql = "select *from product where product.typeid in (select producttype.typeid from producttype where producttype.parentid='"+typeId+"')";
        return DatabaseHelper.queryEntityList(Product.class,sql);
    }

    
    public List<Product> getProductByTypeName(long typeid){
        String sql = "select *from product where typeid='"+typeid+"'";
        return DatabaseHelper.queryEntityList(Product.class,sql);
    }

    /**
     * 获取产品
     */
    public Product getProduct(long id){
        String sql = "SELECT * FROM product WHERE id=?";
        return DatabaseHelper.queryEntity(Product.class,sql,id);
    }

    /**
     * 删除产品
     */
    public boolean deleteProduct(long id){
        return DatabaseHelper.deleteEntity(Product.class,id);
    }

    /**
     * 更新产品
     */
    public boolean updateProduct(long id,Map<String,Object> fieldMap){

        return DatabaseHelper.updateEntity(Product.class,id,fieldMap);
    }

    /**
     * 添加商品
     */
    public boolean addProduct(Map<String,Object> fieldMap){
        return DatabaseHelper.insertEntity(Product.class,fieldMap);
    }

}
